import calculator
from action import *
from thing import *

def cast(actor, target):
    spell,sep,spelltarget = target.partition(" ")
    # Heal spell
    if spelltarget == "": 
        target = actor
        spelltarget = actor.name
    else: 
        target = findTarget(actor, spelltarget)
        if target == None:
            actor.hear("There is nothing here by that name", color.red)
            return
    
    if spell == "heal" or spell == 'hea':
        heal = int(spelldamage(actor)*.6)
        msgself = str("You use your powers to restore "+str(heal)+" health!")
        msgselftgt = str("You use your powers to restore "+target.name+" of "+str(heal)+" health!")
        msgtarget = str(actor.name+" uses their powers to restore your health by "+str(heal)+"!")
        msgroomtgt = str(actor.name+" casts a healing spell on "+target.name+"!")
        msgroomself = str(actor.name+" uses magic to heal!")
        spellattrs = {'type':{'health':{'numeral':heal}},'cost':4, 'msgs':msgself,
        'msgt':msgtarget, 'msgrt':msgroomtgt, 'msgst':msgselftgt, 'allowedtarget':['player'], 'element':'divine', 
        'target':spelltarget, 'msgrs':msgroomself, 'msgcolor':color.blue, 'name':'Minor Healing'}
        spellmaker(actor, spellattrs)
        
    if spell == 'harm':
        damage = int(spelldamage(actor)*.6)
        msgself = str("You use your powers to harm yourself for "+str(damage)+" damage!")
        msgselftgt = str("You use your powers to harm "+target.name+" for "+str(damage)+" damage!")
        msgtarget = str(actor.name+" uses their powers to harm you for "+str(damage)+" damage!")
        msgroomtgt = str(actor.name+" casts harm on "+target.name+"!")
        msgroomself = str(actor.name+" casts a self-damaging spell!")
        spellattrs = {'type':{'health':{'numeral':damage*-1}},'cost':4, 'msgs':msgself,
        'msgt':msgtarget, 'msgrt':msgroomtgt, 'msgst':msgselftgt, 'allowedtarget':['any'], 'element':'divine', 
        'target':spelltarget, 'msgrs':msgroomself, 'msgcolor':color.red, 'name':'Minor Harm'}
        spellmaker(actor, spellattrs)
        
    if spell == 'bless' or spell == 'bles':
        msgself = "You call forth a mighty blessing from the heavens!"
        msgselftgt = str("You call forth a mighty blessing from the heavens on "+target.name+"!")
        msgtarget = str(actor.name+" calls forth a mighty blessing on you from the heavens!")
        msgroomtgt = str(actor.name+" calls forth a mighty blessing on "+target.name+"!")
        msgroomself = str(actor.name+" calls forth a mighty blessing from the heavens!")
        spellattrs = {'type':{'bless':0},'cost':10, 'msgs':msgself, 'msgt':msgtarget, 'msgrt':msgroomtgt, 
        'msgst':msgselftgt, 'duration':400, 'spellbonus':{'dodge':1, 'accuracy':3, 'damage':3}, 
        'attseffect':{'maxHP':5, 'HP':5},'allowedtarget':['player'], 'specialeffect':{'hold': 0}, 
        'element':'divine', 'ticker':{'tf':True, 'tick':50, 'effect':{'HP':1}}, 'target':spelltarget, 
        'msgrs':msgroomself, 'msgcolor':color.green, 'name':'bless', 'statsbonus':{'str':1}}
        spellmaker(actor, spellattrs)
    if spell == 'dispellbless':
        msgself = "You no longer feel blessed"
        msgselftgt = ""
        msgtarget = ""
        msgroomtgt = ""
        msgroomself = ""
        spellattrs = {'type':{'bless':0},'cost':0, 'msgs':msgself, 'msgt':msgtarget, 'msgrt':msgroomtgt, 
        'msgst':msgselftgt,'spellbonus':{'dodge':-1, 'accuracy':-3, 'damage':-3}, 'attseffect':{'maxHP':-5, 'HP':-5},
        'allowedtarget':['selfonly'], 'specialeffect':{'hold': 0}, 'element':'divine', 'ticker':{'tf':False, 'tick':0, 'effect':None},
        'target':spelltarget, 'msgrs':msgroomself, 'msgcolor':color.green, 'name':'dispell', 'statsbonus':{'str':-1}}
        spellmaker(actor, spellattrs)
    
    if spell == 'lbol':
        damage = int(spelldamage(actor)*.7)
        msgself = str("You summon power from the elemental planes and bring lighning down on yourself for "+str(damage)+" damage!")
        msgselftgt = str("You summon power from the elemental planes and bring lighning down on "+target.name+" for "+str(damage)+" damage!")
        msgtarget = str(actor.name+" summons power from the elemental planes and brings lighning down on you for "+str(damage)+" damage!")
        msgroomtgt = str(actor.name+" summons power from the elemental planes and brings lighning down on "+target.name+"!")
        msgroomself = str(actor.name+" casts a self-damaging spell!")
        spellattrs = {'type':{'health':{'numeral':damage*-1}},'cost':5, 'msgs':msgself,
        'msgt':msgtarget, 'msgrt':msgroomtgt, 'msgst':msgselftgt, 'allowedtarget':['any'], 'element':'elemental', 
        'target':spelltarget, 'msgrs':msgroomself, 'msgcolor':color.red, 'name':'Lightning Bolt'}
        spellmaker(actor, spellattrs)
        
    if spell == 'hold':
        duration = int(spelldamage(actor)*3)
        msgself = ""
        msgselftgt = str("You cast hold person on "+target.name+"!")
        msgtarget = str(actor.name+" casts hold person on you. Ethereal binds are constricting your movement!")
        msgroomtgt = str(actor.name+" casts hold person on "+target.name+"!")
        msgroomself = ""
        spellattrs = {'type':{'bless':0,'special':0},'cost':6, 'msgs':msgself, 'msgt':msgtarget, 'msgrt':msgroomtgt, 
        'msgst':msgselftgt, 'duration':duration, 'spellbonus':{'dodge':-3, 'accuracy':-2, 'damage':-1}, 
        'attseffect':{},'allowedtarget':['not self'], 'specialeffect':{'hold': 1}, 
        'element':'divine', 'ticker':{'tf':True, 'tick':30, 'effect':{'HP':-1}}, 'target':spelltarget, 
        'msgrs':msgroomself, 'msgcolor':color.magenta, 'name':'HoldPerson', 'statsbonus':{}}
        spellmaker(actor, spellattrs)
    if spell == 'dispellHoldPerson':
        msgself = "The ethereal binds constricting you fade away."
        msgselftgt = ""
        msgtarget = ""
        msgroomtgt = ""
        msgroomself = ""
        spellattrs = {'type':{'bless':0, 'special':0},'cost':0, 'msgs':msgself, 'msgt':msgtarget, 'msgrt':msgroomtgt, 
        'msgst':msgselftgt,'spellbonus':{'dodge':3, 'accuracy':2, 'damage':1}, 'attseffect':{},
        'allowedtarget':['selfonly'], 'specialeffect':{'hold': -1}, 'element':'divine', 'ticker':{'tf':False, 'tick':0, 'effect':None},
        'target':spelltarget, 'msgrs':msgroomself, 'msgcolor':color.green, 'name':'dispell', 'statsbonus':{}}
        spellmaker(actor, spellattrs)

#heal and damage could be the same essentially, just a negative range for healing and positive for damage
#types: health{'numeral':value}, mana{'numeral':value}, bless:0(anything that does something for a duration will need this), 
#ticker use to tick hp or mp, special(hold, slow, blind, etc using specialeffect), 
#each duration spell will need to have a counterpart dispell spell to call when it wears off and the 'name' must :'dispell'
def spellmaker(actor, spellattrs):
    if spellattrs['cost'] > actor.MP:
        actor.hear("You lack the required focus to cast this spell!", color.red)
        return
    if actor.timers['cooldown'] > 1 and spellattrs['name'] != 'dispell':
        actor.hear("You're still recovering your focus from the last casting.", color.red)
        return
    msgself = spellattrs['msgs']
    msgselftgt = spellattrs['msgst']
    msgtarget = spellattrs['msgt']
    msgroomtgt = spellattrs['msgrt']
    msgroomself = spellattrs['msgrs']
    target = None
    room = fetch(actor.location)
    msgcolor = spellattrs['msgcolor']
    #check for spells allowed targets and set target and the message based on the spells attrs
    if 'selfonly' in spellattrs['allowedtarget']:
        target = actor
        actor.hear(msgself, msgcolor)
        room.broadcast(msgroomself, msgcolor, actor)
    if 'player' in spellattrs['allowedtarget']:
        target = findTarget(actor, spellattrs['target'])
        if target == None: return # Temporary fix, as this crashed on me earlier while trying to heal myself.
        if 'player' not in target.attrs:
            actor.hear("That is not a valid target.", color.red)
            return
        if target == actor:
            actor.hear(msgself, msgcolor)
            room.broadcast(msgroomself, msgcolor, actor)
        else:
            actor.hear(msgselftgt, msgcolor)
            target.hear(msgtarget, msgcolor) 
            room.broadcast(msgroomtgt, msgcolor, actor, target)
    if 'mobonly' in spellattrs['allowedtarget']:
        target = findTarget(actor, spellattrs['target'])
        if 'mob' not in target.attrs:
            actor.hear("That is not a valid target.", color.red)
            return
        actor.hear(msgselftgt, msgcolor)
        room.broadcast(msgroomtgt, msgcolor, actor)
    if 'any' in spellattrs['allowedtarget']:
        target = findTarget(actor, spellattrs['target'])
        if target == actor:
            actor.hear(msgself, msgcolor)
            room.broadcast(msgroomself, msgcolor, actor,)
        else:
            actor.hear(msgselftgt, msgcolor)
            if 'mob' not in target.attrs:
                target.hear(msgtarget, msgcolor)
            room.broadcast(msgroomtgt, msgcolor, actor, target)
    if 'not self' in spellattrs['allowedtarget']:
        target = findTarget(actor, spellattrs['target'])
        if target == actor:
            actor.hear("You can't target yourself with this spell!",color.red)
            return
        else:
            actor.hear(msgselftgt, msgcolor)
            if 'mob' not in target.attrs:
                target.hear(msgtarget, msgcolor)
            room.broadcast(msgroomtgt, msgcolor, actor, target)
    if 'room' in spellattrs['allowedtarget']:
        target = 'room'
        actor.hear(msgself, msgcolor)
        room.broadcast(msgtarget, msgcolor, actor)
    if 'party' in spellattrs['allowedtarget']:
        target = 'party'
        actor.hear(msgself, msgcolor)
        room.broadcast(msgtarget, msgcolor, actor)
    if 'enemyroom' in spellattrs['allowedtarget']:
        target = 'enemyroom'
        actor.hear(msgself, msgcolor)
        room.broadcast(msgtarget, msgcolor, actor)
    #we now have a valid target and enough mana to cast the spell
    if target != None: 
        actor.MP -= spellattrs['cost']
        actor.timers['cooldown'] = 30
        if 'bless' in spellattrs['type']:
            timername = spellattrs['name']
            if timername in target.blessSpells and spellattrs['name'] != 'dispell':
                target.spellTimers[timername] = {'duration':spellattrs['duration'], 'tick': 0, 'name':timername}
            if spellattrs['name'] != "dispell":
                target.spellTimers[timername] = {'duration':spellattrs['duration'], 'tick': 0, 'name':timername}
            if timername not in target.blessSpells:
                if spellattrs['name'] != "dispell":
                    target.blessSpells.append(target.spellTimers[timername]['name'])
                for enhancement in spellattrs['spellbonus'].keys():
                    target.spellBonus[enhancement] += spellattrs['spellbonus'][enhancement]
                for enhancement in spellattrs['attseffect'].keys():
                    stupid = []
                    stupid.append(enhancement)
                    for thing in stupid:
                        if thing == 'HP':
                            target.HP += spellattrs['attseffect'][enhancement]
                        if thing == 'MP':
                            target.MP += spellattrs['attseffect'][enhancement]
                        if thing == 'maxHP':
                            target.maxHP += spellattrs['attseffect'][enhancement]
                for enhancement in spellattrs['statsbonus'].keys():
                    target.baseStats[enhancement] += spellattrs['statsbonus'][enhancement]
                if 'special' in spellattrs['type']:
                    for effect in spellattrs['specialeffect']:
                        if effect == "hold":
                            target.miscAttrs['held'] += spellattrs['specialeffect']['hold']
                        if effect == "blind":
                            target.miscAttrs['blind'] += spellattrs['specialeffect']['blind']
                        #add more of these as called for
                if spellattrs['ticker']['tf']:
                    target.spellTimers["tick"+timername] = {'duration':spellattrs['ticker']['tick'], 'tick':0, 'name':'tick'+timername, 'effect':spellattrs['ticker']['effect']}
        if 'health' in spellattrs['type']:
            health = spellattrs['type']['health']['numeral']
            healthdif = target.maxHP-target.HP
            if healthdif < health:
                target.HP = target.maxHP
            else: target.HP += health
            prompt(target)
            actor.deathCheck(target)
        if 'mana' in spellattrs['type']:
            mana = spellattrs['type']['mana']['numeral']
            manadif = target.maxHP-target.HP
            if manadif < mana:
                target.MP = target.maxMP
            else: target.MP += mana
            prompt(target)

